//
//  CPSCGradualChangeBtn.m
//  CollTest
//
//  Created by codew on 11/22/21.
//

#import "CPSCGradualChangeBtn.h"

@interface CPSCGradualChangeTools : NSObject

@property (nonatomic, strong) NSMutableDictionary * dictImageM;

- (UIImage*)imageWithColor1:(NSString *)color1  color2:(NSString *)color2;
@end

@implementation CPSCGradualChangeTools

+ (instancetype)shared {
    
    static CPSCGradualChangeTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[CPSCGradualChangeTools alloc] init];
        
    });
    return instance;
}

- (NSMutableDictionary *)dictImageM
{
    if (!_dictImageM) {
        
        _dictImageM = [NSMutableDictionary dictionary];
    }
    
    return _dictImageM;
}

- (UIImage*)imageWithColor1:(NSString *)color1  color2:(NSString *)color2
{
    NSString * key = [NSString stringWithFormat:@"%@%@", color1, color2];
    UIImage * image = [self.dictImageM valueForKeyPath:key];
    if (image) {
        return image;
    }
    
    CGSize size = CGSizeMake(58, 14);
    image = [self imageWithUISize:size color1:[UIColor colorWithHexString:color1] color2:[UIColor colorWithHexString:color2]];
    
    [self.dictImageM setValue:image forKey:key];
    
    return image;
}

//58x14
//view转成image
- (UIImage*)imageWithUISize:(CGSize)size color1:(UIColor *)color1  color2:(UIColor *)color2 {
    
    /**
     PS: @3x
     注意 ：创建这个Label的时候，frame，font，cornerRadius要设置成所生成的图片的3倍，也就是说要生成一个三倍图，否则生成的图片会虚
     */
    CGFloat countQuanNX = 3;
    CGSize sizeQuan3X = CGSizeMake(size.width * countQuanNX, size.height * countQuanNX);
    
    // ================================
    UIView * viewSuper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sizeQuan3X.width, sizeQuan3X.height)];
    
    
    //=======
//    viewSuper.backgroundColor = kCPSCRandomColor;
    
    // ================================================================
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = viewSuper.bounds;
    // 设置渐变颜色数组
//    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#ED6242"].CGColor,
//                             (__bridge id)[UIColor colorWithHexString:@"#ED2929"].CGColor];
    
    gradientLayer.colors = @[(__bridge id)color1.CGColor,
                             (__bridge id)color2.CGColor];
    // 设置渐变起始点
    gradientLayer.startPoint = CGPointMake(.0, .0);
    // 设置渐变结束点
    gradientLayer.endPoint = CGPointMake(.0, 1.0);
    gradientLayer.cornerRadius = 2*countQuanNX;
    
    // ================================================================
    viewSuper.layer.cornerRadius = 2*countQuanNX;
    [viewSuper.layer addSublayer:gradientLayer];
    
    
    // ================================================================
    UIGraphicsBeginImageContext(viewSuper.bounds.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [viewSuper.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tImage;
}

@end


@interface CPSCGradualChangeBtn ()

@property (nonatomic, strong) NSString * color1;
@property (nonatomic, strong) NSString * color2;

@property (nonatomic, strong) UIImage * imageGradualChange;
@end

@implementation CPSCGradualChangeBtn

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)addGradualChangeColor1:(NSString *)color1  color2:(NSString *)color2
{
    self.color1 = color1;
    self.color2 = color2;
    
    UIImage * image = [[CPSCGradualChangeTools shared] imageWithColor1:color1 color2:color2];
    [self setBackgroundImage:image forState:UIControlStateNormal];
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//
//
//    if (self.color1 && self.color2 && !self.imageGradualChange) {
//
//        UIImage * imageBG = [CPSCGradualChangeBtn imageWithUISize:self.size color1:self.color1 color2:self.color2];
//        [self setBackgroundImage:imageBG forState:UIControlStateNormal];
//        self.imageGradualChange = imageBG;
//    }
//}



@end

